package com.sm.controller;

import com.sm.entity.ResultMap;
import com.sm.entity.Welfare;
import com.sm.service.WelfareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin/welfare")
public class WelfareController {

    @Autowired
    ResultMap resultMap;
    @Autowired
    WelfareService welfareService;

    @RequestMapping("/addwelfare")
    public String addWelfare(){
        return "admin/personnel/addwelfare";
    }

    @ResponseBody
    @RequestMapping("/addwelfare.do")
    public ResultMap addWelfareDo(Welfare welfare)throws Exception{
        welfareService.addWelfare(welfare);
        resultMap.setMessage("添加成功");
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/addweluser.do")
    public ResultMap addWelfareForUser( Integer uid,@RequestParam(required = true) Integer wid)throws Exception{
        welfareService.addWelfareForUser(uid, wid);
        resultMap.setMessage("添加成功");
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/findallwel")
    public ResultMap findAllWel()throws Exception{
        resultMap.setResult("welfares",welfareService.findAllWelfare());
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/findwelfare")
    public ResultMap findWelfare(Integer uid)throws Exception{
        resultMap.setResult("userwelfare",welfareService.findWelfareByUid(uid));
        return resultMap;
    }


    @ResponseBody
    @RequestMapping("/searchuser")
    public ResultMap searchUser(Integer flag,String value)throws Exception{
        if(value == null ||value.equals("")){
            resultMap.setMessage("查询条件错误");
            return resultMap;
        }
        if(flag == 1) {
            resultMap.setResult("users",welfareService.findUser(value,null));
        }else if(flag == 2){
            resultMap.setResult("users",welfareService.findUser(null,value));
        }else {
            resultMap.setMessage("查询条件错误");
            return resultMap;
        }
        return resultMap;
    }
}
